package marksys.free.com.marksys.ui.fragment.diligent;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;
import android.os.Build;
import android.support.annotation.MainThread;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.inject.Inject;
import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.common.Status;
import marksys.free.com.marksys.nettool.vo.ScoreRecords;
import marksys.free.com.marksys.repo.HistoryRepo;
import marksys.free.com.marksys.ui.fragment.ObjectUtils;
import timber.log.Timber;

public class RecordDiligentViewModel extends ViewModel {
    private HistoryRepo historyRepo;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    private final MutableLiveData<RecordDiligentViewModel.Query> keyLiveData = new MutableLiveData<>();
    public final LiveData<Resource<List<ScoreRecords>>> historyDiligentLiveData = Transformations.switchMap(keyLiveData, new Function<RecordDiligentViewModel.Query, LiveData<Resource<List<ScoreRecords>>>>() {
        @Override
        public LiveData<Resource<List<ScoreRecords>>> apply(RecordDiligentViewModel.Query query) {
            return historyRepo.history(4);
        }
    });
    public final LiveData<Resource<List<RecordDiligentViewModel.DiligentDayRecord>>> Diligentdata = Transformations.map(historyDiligentLiveData, (Resource<List<ScoreRecords>> Diligentinput) -> {
        switch (Diligentinput.status) {
            case ERROR:
                return Resource.error(Diligentinput.errorCode, Diligentinput.errorMessage, null);
            case SUCCESS:
                List<RecordDiligentViewModel.DiligentDayRecord> DiligentdayRecords = new ArrayList<>();
                Map<String, Map<String, List<ScoreRecords>>> result = new LinkedHashMap<>();
                Map<String, List<ScoreRecords>> tempTimeMap;
                List<ScoreRecords> tempScoreList;
                assert Diligentinput.data != null;
                for (ScoreRecords Diligentrecord : Diligentinput.data) {
                    String time = sdf.format(Diligentrecord.time);
                    tempTimeMap = result.get(time);
                    if (tempTimeMap == null) {
                        tempTimeMap = new LinkedHashMap<>();
                        result.put(time, tempTimeMap);
                    }
                    tempScoreList = tempTimeMap.get(Diligentrecord.className);
                    if (tempScoreList == null) {
                        tempScoreList = new ArrayList<>();
                        tempTimeMap.put(Diligentrecord.className, tempScoreList);
                    }
                    tempScoreList.add(Diligentrecord);
                }

                for (String k : result.keySet()) {
                    Map<String, List<ScoreRecords>> map = result.get(k);

                    RecordDiligentViewModel.DiligentDayRecord dr = new RecordDiligentViewModel.DiligentDayRecord(k);
                    dr.records = new ArrayList<>();
                    DiligentdayRecords.add(dr);

                    for (String j : map.keySet()) {
                        List<ScoreRecords> scoreRecords = map.get(j);
                        RecordDiligentViewModel.DiligentClassRecord cr = new RecordDiligentViewModel.DiligentClassRecord(j,k);
                        cr.DiligentscoreRecords = new ArrayList<>();
                        cr.DiligentscoreRecords.addAll(scoreRecords);
                        dr.records.add(cr);
                    }
                }
                Timber.i("ssssss %s",DiligentdayRecords);
                return Resource.success(DiligentdayRecords);
            default:
                return Resource.loading(null);
        }
    });
    public final MediatorLiveData<Resource<Boolean>> loadMoreStatus = new MediatorLiveData<>();
    public final LiveData<Boolean> hasMoreData;


    @Inject
    RecordDiligentViewModel(HistoryRepo historyRepo) {
        this.historyRepo = historyRepo;
        hasMoreData = historyRepo.hasMoreData();
    }

    @MainThread
    public void setQueryKey(String data) {
        RecordDiligentViewModel.Query query = new RecordDiligentViewModel.Query(data);
        Timber.i("query : %s", query);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Objects.equals(query, keyLiveData.getValue())) {
                return;
            }
        }
        keyLiveData.setValue(query);
    }

    @MainThread
    public void refresh() {
        keyLiveData.setValue(keyLiveData.getValue());
    }

    protected class Query {
        String data;

        private Query(String data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "Query{" +
                    "date='" + data + '\'' +
                    '}';
        }

    }

    private volatile boolean isLoading;

    @MainThread
    public void loadMore() {
        if (isLoading)
            return;
        else
            isLoading = true;
        LiveData<Resource<Boolean>> loadMore = historyRepo.loadMore();
        loadMoreStatus.addSource(loadMore, resource -> {
            loadMoreStatus.setValue(resource);
            if (resource != null && resource.status != Status.LOADING) {
                loadMoreStatus.removeSource(loadMore);
                isLoading = false;
            }
        });
    }


     //日记录

    public static class DiligentDayRecord {

        public DiligentDayRecord() {

        }

        public DiligentDayRecord(String data) {
            this.data = data;
        }

        //日期
        public String data;

        public List<DiligentClassRecord> records;

        @Override
        public String toString() {
            return "DiligentDayRecord{" +
                    "data='" + data + '\'' +
                    ", records=" + records +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DiligentDayRecord that = (DiligentDayRecord) o;
            return ObjectUtils.equals(data, that.data) &&
                    ObjectUtils.equals(records, that.records);
        }

    }


     //班级记录

    public static class DiligentClassRecord {

        public DiligentClassRecord() {

        }

        public DiligentClassRecord(String DiligentclassName,String Diligenttime) {
            this.DiligentclassName = DiligentclassName;
            this.Diligenttime = Diligenttime;
        }

        public String Diligenttime;
        //名字
        public String DiligentclassName;

        public List<ScoreRecords> DiligentscoreRecords;

        @Override
        public String toString() {
            return "DiligentClassRecord{" +
                    "DiligentclassName='" + DiligentclassName + '\'' +
                    ", DiligentscoreRecords=" + DiligentscoreRecords +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DiligentClassRecord that = (DiligentClassRecord) o;
            return ObjectUtils.equals(Diligenttime, that.Diligenttime) &&
                    ObjectUtils.equals(DiligentclassName, that.DiligentclassName) &&
                    ObjectUtils.equals(DiligentscoreRecords, that.DiligentscoreRecords);
        }

    }

}

